package org.kaka.service.impl;

import org.kaka.dto.ChapterDto;
import org.kaka.dto.ContentDto;
import org.kaka.mapper.ChapterMapper;
import org.kaka.mapper.ContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentServiceImpl {
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    public ContentDto detail(ContentDto contentDto) {
        ContentDto detail = contentMapper.detail(contentDto);
        if(detail!=null){
            ChapterDto chapterDto = chapterMapper.selectById(contentDto.getChapterId());
            detail.setChapterIndex(chapterDto.getIndex());
            detail.setChapterTitle(chapterDto.getTitle());
        }
        return detail;
    }

    public void save(ContentDto contentDto) {
        ChapterDto chapterDto=new ChapterDto();
        chapterDto.setTitle(contentDto.getChapterTitle());
        chapterDto.setIndex(contentDto.getChapterIndex());
        chapterDto.setBookId(contentDto.getBookId());
        chapterMapper.insert(chapterDto);
        contentDto.setChapterId(chapterDto.getId());
        contentMapper.insert(contentDto);
    }

    public void update(ContentDto contentDto) {
        if(contentDto.getChapterTitle()!=null||contentDto.getChapterIndex()!=null){
            ChapterDto chapterDto=new ChapterDto();
            chapterDto.setId(contentDto.getChapterId());
            if(contentDto.getChapterTitle()!=null){
                chapterDto.setTitle(contentDto.getChapterTitle());
            }
            if(contentDto.getChapterIndex()!=null){
                chapterDto.setIndex(contentDto.getChapterIndex());
            }
            chapterMapper.updateById(chapterDto);
        }
        contentMapper.updateById(contentDto);
    }

    public void delete(Long id) {
        contentMapper.deleteById(id);
    }
}
